package com.ms.oschina.fragment;

import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ms.oschina.R;
import com.ms.oschina.base.BaseDetailsActivity;
import com.ms.oschina.base.BaseFragment;
import com.ms.oschina.emoji.MyOnExpressionSelectListener;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;

public class NewsDetailBottomEmojiFragment extends BaseFragment implements
		OnClickListener {

	private TextView tvCommentCount;
	private FrameLayout flNewsdetailOperation;
	private ImageView ivNewsdetailWriteComment1;
	private RelativeLayout ivNewsdetailLookComment;
	private TextView tvDetailsCommentCount;
	private ImageView ivNewsdetailWriteComment2;
	private ImageView ivNewsdetailCollect;
	private ImageView ivNewsdetailShare;
	private FrameLayout flNewsdetailComment;
	private ImageView ivNewsdetailBackOperation;
	private EditText etNewsdetailCommentContent;
	private ImageView ivNewsdetailExpression;
	private ImageView etNewsdetailSend;

	private ExpressionFragment expressionFragment;
	private FragmentTransaction beginTransaction;
	private View view_line;

	private void findViews(View view) {
		flNewsdetailOperation = (FrameLayout) view
				.findViewById(R.id.fl_newsdetail_operation);
		ivNewsdetailWriteComment1 = (ImageView) view
				.findViewById(R.id.iv_newsdetail_writeComment1);
		ivNewsdetailLookComment = (RelativeLayout) view
				.findViewById(R.id.rl_newsdetail_lookComment);
		tvDetailsCommentCount = (TextView) view
				.findViewById(R.id.tv_details_commentCount);
		ivNewsdetailWriteComment2 = (ImageView) view
				.findViewById(R.id.iv_newsdetail_writeComment2);
		ivNewsdetailCollect = (ImageView) view
				.findViewById(R.id.iv_newsdetail_collect);
		ivNewsdetailShare = (ImageView) view
				.findViewById(R.id.iv_newsdetail_share);
		flNewsdetailComment = (FrameLayout) view
				.findViewById(R.id.fl_newsdetail_comment);
		ivNewsdetailBackOperation = (ImageView) view
				.findViewById(R.id.iv_newsdetail_back_operation);
		ivNewsdetailBackOperation.setVisibility(View.VISIBLE);
		etNewsdetailCommentContent = (EditText) view
				.findViewById(R.id.et_newsdetail_commentContent);
		ivNewsdetailExpression = (ImageView) view
				.findViewById(R.id.iv_newsdetail_expression);
		etNewsdetailSend = (ImageView) view
				.findViewById(R.id.et_newsdetail_send);
		tvCommentCount = (TextView) view.findViewById(R.id.tv_details_commentCount);

		view_line = (View) view.findViewById(R.id.view_line);
	}

	@Override
	public int getLayoutId() {
		return R.layout.fragment_newsdetailbottom;
	}

	@Override
	public void initView(View view) {
		findViews(view);
		if (onCreateListener != null) {
			onCreateListener.OnCreate();
		}
	}

	public void setCommentCount(int commentCount) {

		if (commentCount == 0) {
			tvCommentCount.setVisibility(View.INVISIBLE);
		} else {
			tvCommentCount.setVisibility(View.VISIBLE);
			tvCommentCount.setText("" + commentCount);
		}
	}

	@Override
	protected void initData() {
		// 设置点击事件
		ivNewsdetailWriteComment1.setOnClickListener(this);
		ivNewsdetailWriteComment2.setOnClickListener(this);
		ivNewsdetailLookComment.setOnClickListener(this);
		ivNewsdetailCollect.setOnClickListener(this);
		ivNewsdetailShare.setOnClickListener(this);
		ivNewsdetailBackOperation.setOnClickListener(this);
		ivNewsdetailExpression.setOnClickListener(this);
		etNewsdetailSend.setOnClickListener(this);

		detailsActivity = (BaseDetailsActivity) getActivity();
		MyOnExpressionSelectListener listener = new MyOnExpressionSelectListener(
				etNewsdetailCommentContent);
		expressionFragment = new ExpressionFragment(listener);
		expressionFragment.setDelEditText(etNewsdetailCommentContent);
		beginTransaction = detailsActivity.getSupportFragmentManager()
				.beginTransaction();
		beginTransaction.replace(R.id.fl, expressionFragment);
		beginTransaction.hide(expressionFragment);
		beginTransaction.commit();
	}

	private Boolean isExpressionColse = true;
	private BaseDetailsActivity detailsActivity;
	private OnCreateListener onCreateListener;

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.et_newsdetail_send:// 发送
			break;
		case R.id.iv_newsdetail_expression:// 打开或关闭表情
			if (isExpressionColse) {
				openExpression();
			} else {
				closeExpression();
			}
			isExpressionColse = !isExpressionColse;
			break;
		case R.id.iv_newsdetail_back_operation: // 关闭评论面板
			closeExpression();
			closeCommentPanel(true);
			break;
		case R.id.iv_newsdetail_share:
			break;
		case R.id.iv_newsdetail_collect:// 收藏
			break;
		case R.id.iv_newsdetail_writeComment1:// 填写评论
			// 打开评论面板
			openCommentPanel(true);
			break;
		case R.id.iv_newsdetail_writeComment2:// 填写评论
			openCommentPanel(true);
			break;
		case R.id.rl_newsdetail_lookComment:// 查看评论
			// UiUtils.lookComment();
			break;
		}
	}

	public void setHindConvertView() {
		view_line.setVisibility(View.GONE);
		ivNewsdetailBackOperation.setVisibility(View.GONE);
	}

	/**
	 * 打开表情
	 */
	private void openExpression() {
		// 切换笑脸颜色为绿色
		ivNewsdetailExpression.setImageResource(R.drawable.btn_emoji_pressed);

		beginTransaction = detailsActivity.getSupportFragmentManager()
				.beginTransaction();
		beginTransaction.show(expressionFragment);
		beginTransaction.commit();
	}

	/**
	 * 关闭表情
	 */
	private void closeExpression() {
		ivNewsdetailExpression.setImageResource(R.drawable.btn_emoji_normal);

		beginTransaction = detailsActivity.getSupportFragmentManager()
				.beginTransaction();
		beginTransaction.hide(expressionFragment);
		beginTransaction.commit();

	}

	/**
	 * 
	 * @param ani
	 *            是否需要动画
	 */
	public void closeCommentPanel(boolean ani) {
		if (ani) {
			doExitAni(flNewsdetailComment, flNewsdetailOperation);
		} else {
			closeOne(flNewsdetailComment, flNewsdetailOperation);
		}
	}

	/**
	 * 
	 * @param ani
	 *            是否需要动画
	 */
	public void openCommentPanel(boolean ani) {
		if (ani) {
			// 退出动画
			doExitAni(flNewsdetailOperation, flNewsdetailComment);
		} else {
			closeOne(flNewsdetailOperation, flNewsdetailComment);
		}
	}

	/**
	 * 退出动画
	 * 
	 * @param view1
	 *            哪个view退出
	 * @param view2
	 *            哪个view 显示
	 */
	private void doExitAni(final View view1, final View view2) {
		// 必须清空监听器，要不然会无限回调
		ViewPropertyAnimator.animate(view1).setListener(null);
		ViewPropertyAnimator.animate(view2).setListener(null);
		ViewPropertyAnimator.animate(view1).translationY(view1.getHeight())
				.setListener(new AnimatorListener() {

					@Override
					public void onAnimationStart(Animator arg0) {
					}

					@Override
					public void onAnimationRepeat(Animator arg0) {
					}

					@Override
					public void onAnimationEnd(Animator arg0) {
						closeOne(view1, view2);

//						ViewHelper.setTranslationY(view2, view2.getHeight());

						ViewPropertyAnimator.animate(view2).translationY(0)
								.start();
					}

					@Override
					public void onAnimationCancel(Animator arg0) {
					}
				}).start();
//		TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);
//		ta.setDuration(1000);
//		view1.startAnimation(ta);
	}

	/**
	 * 快速隐藏一个view ，并显示另一个view
	 * 
	 * @param view1
	 *            隐藏的view
	 * @param view2
	 *            显示的view
	 */
	private void closeOne(final View view1, final View view2) {
		view1.setVisibility(View.GONE);
		view2.setVisibility(View.VISIBLE);
	}

	// OnExpressionSelectListener listener = new OnExpressionSelectListener() {
	//
	// @Override
	// public void OnExpressionSelect(DisplayRules displayRules) {
	// // 设置图片大小
	// // ForegroundColorSpan span = new
	// // ForegroundColorSpan(Color.BLUE);//让文字变成蓝色
	// // StyleSpan span = new StyleSpan(Typeface.BOLD_ITALIC); //设置斜体
	// // URLSpan span = new URLSpan("http://www.baidu.com");
	// SpannableString spannableString = new SpannableString(
	// displayRules.getEmojiStr());// 参数1：这个表情在edittext中显示的文本
	// // 当前的displayRules.getRemote()文字： start：从哪个文字角标开始 end：
	// // 设置到哪几个文字使用span
	// ImageSpan span = EmojiUtils.getEmojiImgSpan(
	// displayRules.getResId(),
	// UiUtils.getDimensionPixelSize(R.dimen.space_20));
	// spannableString.setSpan(span, 0, displayRules.getRemote().length(),
	// Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
	//
	// // 添加文字到edittext中
	// etNewsdetailCommentContent.append(spannableString);
	// UiUtils.showToast(etNewsdetailCommentContent.getText().toString());
	// }
	// };

	public boolean autoLoadData() {
		return true;
	}

	public interface OnCreateListener {
		void OnCreate();
	}

	public void setOnCreateListener(OnCreateListener onCreateListener) {
		this.onCreateListener = onCreateListener;

	}
}
